<script setup>
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { loginStore } from '@/stores/login'
import request from '@/utils/request'
const login = loginStore();
const router = useRouter()

const agree = ref(false)
const formModel = ref({
    username: '',
    password: ''
})
const rules = ref({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 4, max: 20, message: '用户名长度在 4 到 20个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { pattern: /^\S{6,12}$/, message: '密码格式不正确', trigger: 'blur' }
    ]
})

const formRef = ref()
const code = () => {
    if (login.token) {
        router.push('/')
        ElMessage({
            type: 'success',
            message: '登录成功'
        })
    } else {
        ElMessage({
            type: 'warning',
            message: '用户名密码错误！'
        })
    }
}
const doLogin = async (formEl) => {
    if (agree.value !== true) {
        ElMessage({
            type: 'warning',
            message: '请勾选协议！',
        })
    } else {
        await formEl.validate(async (valid, fields) => {
            if (valid) {
                await login.setToken(formModel.value)
                code()
            } else {
                ElMessage({
                    type: 'warning',
                    message: '用户名密码格式错误！'
                })
            }
        })
    }
}

const drawer = ref(false)

const show = () => {
    drawer.value = true
}

const doRegister = async (formEl) => {
    await formEl.validate(async (valid, fields) => {
        if (valid) {
            const res = await request.get('/check', {
                params: {
                    username: formModel.value.username,
                }
            })
            console.log(res.data.data)
            if (res.data.data == "用户名已存在") {
                ElMessage({
                    type: 'warning',
                    message: '用户名存在！'
                })
            } else {
                request.post('/register', formModel.value)
                ElMessage({
                    type: 'success',
                    message: '注册成功，请登录'
                })
                drawer.value = false
            }
        } else {
            ElMessage({
                type: 'warning',
                message: '用户名密码格式错误！'
            })
        }
    })
}

</script>


<template>
    <div>
        <header class="login-header">
            <div class="container m-top-20">
                <h1 class="logo">
                    <RouterLink to="/">逐火书城</RouterLink>
                </h1>
                <!-- <RouterLink class="entry" to="/">
                    进入首页
                </RouterLink> -->
                <RouterLink class="entry" to="/emplogin">
                    员工登录
                </RouterLink>
            </div>
        </header>
        <section class="login-section">
            <div class="wrapper">
                <nav>
                    <div class="title">用户登录</div>
                </nav>
                <div class="account-box">
                    <div class="form">
                        <el-form ref="formRef" label-position="right" label-width="60px" status-icon :model="formModel"
                            :rules="rules">
                            <el-form-item label="账户" prop="username">
                                <el-input v-model="formModel.username" />
                            </el-form-item>
                            <el-form-item label="密码" prop="password">
                                <el-input v-model="formModel.password" type="password" />
                            </el-form-item>
                            <el-form-item label-width="22px">
                                <el-checkbox size="large" v-model="agree">
                                    我已同意隐私条款和服务条款
                                </el-checkbox>
                            </el-form-item>
                            <a href="#" class="register" @click="show">没有账号？去注册</a>
                            <el-button size="large" class="subBtn" @click="doLogin(formRef)">点击登录</el-button>
                        </el-form>
                    </div>
                </div>
            </div>
        </section>

        <el-drawer v-model="drawer" :with-header="false">
            <div class="reg">用户注册</div>
            <el-form ref="formRef" label-position="right" label-width="60px" status-icon :model="formModel"
                :rules="rules">
                <el-form-item label="账户" prop="username">
                    <el-input v-model="formModel.username" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="formModel.password" type="password" />
                </el-form-item>
            </el-form>
            <el-button size="large" class="subBtn" @click="doRegister(formRef)">注册</el-button>
        </el-drawer>

        <footer class="login-footer">
            <div class="container">
                <p>
                    <a href="javascript:;">关于我们</a>
                    <a href="javascript:;">帮助中心</a>
                    <a href="javascript:;">售后服务</a>
                    <a href="javascript:;">配送与验收</a>
                    <a href="javascript:;">商务合作</a>
                    <a href="javascript:;">搜索推荐</a>
                    <a href="javascript:;">友情链接</a>
                </p>
                <p>CopyRight &copy; 逐火书城</p>
            </div>
        </footer>
    </div>
</template>

<style scoped lang='scss'>
.reg {
    text-align: center;
    color: red;
    font-weight: 800;
    margin-bottom: 8px;
    font-size: 18px;
}

.register {
    display: block;
    text-decoration: underline;
    text-align: center;

    &:hover {
        color: red;
    }
}

.login-header {
    background: white;
    height: 15vh;
    border-bottom: 2px solid rebeccapurple;

    .container {
        display: flex;
        align-items: flex-end;
        justify-content: space-between;
    }

    .logo {
        width: 200px;

        a {
            display: block;
            height: 132px;
            width: 100%;
            text-indent: -9999px;
            background: url("/image/logo.jpg") no-repeat center 18px / contain;
        }
    }

    .sub {
        flex: 1;
        font-size: 24px;
        font-weight: normal;
        margin-bottom: 38px;
        margin-left: 20px;
        color: #666;
    }

    .entry {
        width: 120px;
        margin-bottom: 38px;
        font-size: 16px;
        color: aqua;

        i {
            font-size: 14px;
            color: aqua;
            letter-spacing: -5px;
        }
    }
}

.entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;
    color: aqua;
    margin-left: 260px;

    &:hover {
        color: red;
    }

    i {
        font-size: 14px;
        color: aqua;
        letter-spacing: -5px;
    }
}

.login-section {
    background: url('/image/loginBg.jpg') no-repeat center / cover;
    height: 70vh;
    position: relative;

    .wrapper {
        width: 380px;
        background: #fff;
        position: absolute;
        left: 50%;
        top: 54px;
        transform: translate3d(100px, 0, 0);
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

        nav {
            font-size: 14px;
            height: 55px;
            margin-bottom: 20px;
            border-bottom: 1px solid #f5f5f5;
            display: flex;
            padding: 0 40px;
            text-align: right;
            align-items: center;

            .title {
                flex: 1;
                line-height: 1;
                display: inline-block;
                font-size: 18px;
                position: relative;
                text-align: center;
            }
        }
    }
}

.login-footer {
    height: 15vh;
    background: #3C3C3C;

    p {
        text-align: center;
        color: white;
        padding-top: 20px;

        a {
            line-height: 1;
            padding: 0 10px;
            color: white;
            display: inline-block;
        }

        a:hover {
            color: aqua;
        }
    }
}

.account-box {
    .toggle {
        padding: 15px 40px;
        text-align: right;

        a {
            color: aqua;

            i {
                font-size: 14px;
            }
        }
    }

    .form {
        padding: 0 20px 20px 20px;

        &-item {
            margin-bottom: 28px;

            .input {
                position: relative;
                height: 36px;

                >i {
                    width: 34px;
                    height: 34px;
                    background: #cfcdcd;
                    color: #fff;
                    position: absolute;
                    left: 1px;
                    top: 1px;
                    text-align: center;
                    line-height: 34px;
                    font-size: 18px;
                }

                input {
                    padding-left: 44px;
                    border: 1px solid #cfcdcd;
                    height: 36px;
                    line-height: 36px;
                    width: 100%;

                    &.error {
                        border-color: aqua;
                    }

                    &.active,
                    &:focus {
                        border-color: aqua;
                    }
                }

                .code {
                    position: absolute;
                    right: 1px;
                    top: 1px;
                    text-align: center;
                    line-height: 34px;
                    font-size: 14px;
                    background: #f5f5f5;
                    color: #666;
                    width: 90px;
                    height: 34px;
                    cursor: pointer;
                }
            }

            >.error {
                position: absolute;
                font-size: 12px;
                line-height: 28px;
                color: aqua;

                i {
                    font-size: 14px;
                    margin-right: 2px;
                }
            }
        }

        .agree {
            a {
                color: #069;
            }
        }

        .btn {
            display: block;
            width: 100%;
            height: 40px;
            color: #fff;
            text-align: center;
            line-height: 40px;
            background: aqua;

            &.disabled {
                background: #cfcdcd;
            }
        }
    }

    .action {
        padding: 20px 40px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .url {
            a {
                color: #999;
                margin-left: 10px;
            }
        }
    }
}

.subBtn {
    background: aqua;
    width: 100%;
    color: rebeccapurple;
    transition: .5s linear;
    margin-top: 8px;
}

.subBtn:hover {
    background: rebeccapurple;
    color: aqua;
}
</style>